package com.cloud.drore.eboss.operation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cloud.drore.eboos.common.base.BaseServiceImpl;
import com.cloud.drore.eboos.common.base.MyMapper;

import com.cloud.drore.eboos.common.base.ResultMessage;


import com.cloud.drore.eboos.exception.BusinessException;
import com.cloud.drore.eboss.login.feign.ILoginFeign;
import com.cloud.drore.eboss.login.feign.ShiroUser;
import com.cloud.drore.eboss.operation.entity.Merchant;
import com.cloud.drore.eboss.operation.entity.MerchantManage;
import com.cloud.drore.eboss.operation.entity.vo.MerchantManageVo;
import com.cloud.drore.eboss.operation.mapper.MerchantManageMapper;
import com.cloud.drore.eboss.operation.mapper.MerchantMapper;
import com.cloud.drore.eboss.operation.service.IMerchantManageService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/6 0006 下午 1:08 <br>
 * @Author: <a href="410508961@qq.com">夏凯</a>
 */
@Service
@Slf4j
public class MerchantManageServiceImpl extends BaseServiceImpl implements IMerchantManageService {
    @Autowired
    MerchantManageMapper merchantManageMapper;
    @Autowired
    MerchantMapper merchantMapper;
    @Autowired
    ILoginFeign iLoginFeign;
    private static final int DEALER_FEGIN_SERVICE_SUCCESS_CODE = 8200;

    @Override
    public MyMapper getMapper() {
        return merchantManageMapper;
    }

    /**
     * @param merchantManage
     * @ClassName: MerchantManageServiceImpl
     * @Return: int
     * @Decription: 设置管理员
     * @CreateDate: Created in 2018/1/18 0018 下午 6:43
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    @Transactional(rollbackFor = BusinessException.class)
    public int addMerchantManage(MerchantManage merchantManage) throws BusinessException {
        Merchant merchant = merchantMapper.selectByPrimaryKey(merchantManage.getMerchantId());
        if (null == merchant || "".equals(merchant)) {
            throw new RuntimeException("账户信息错误！");
        }
        if (merchant.getMerchantType().equals("MERCHANT_DISTRIBUTE")) {
            if (!(merchant.getMerchantIndustry().equals("MERCHANT_INDUSTRY_HOUSEKEEPER"))) {
                throw new RuntimeException("当前账户不能添加超级管理员!");
            }
        }

        //给商户表增加管理员
        MerchantManage afterInsertMerchantManage = (MerchantManage) insert(merchantManage);
        int flagSuccess = merchantManageMapper.addMerchantManageSql(afterInsertMerchantManage.getId(), merchantManage.getMerchantId());
        //给供应商添加超级管理员
        ResultMessage<Integer> resultMessage = iLoginFeign.addUser(addMerchantManager(merchantManage));
        if (resultMessage.getCode() != DEALER_FEGIN_SERVICE_SUCCESS_CODE) {
            throw new BusinessException(Integer.parseInt(String.valueOf(resultMessage.getCode())), resultMessage.getMessage().toString());
        }
        //插入管理员信息表
        // merchantManage.setUserId(Long.parseLong(resultMessage.getData().toString()));
        merchantManageMapper.updateMerchantManagerUserId(afterInsertMerchantManage.getMerchantId(), Long.parseLong(resultMessage.getData().toString()));
        return flagSuccess;
    }

    /**
     * @param merchantManage
     * @ClassName: MerchantManageServiceImpl
     * @Return: com.cloud.drore.eboss.login.feign.ShiroUser
     * @Decription: 设置管理员在供应商那边的商户信息
     * @CreateDate: Created in 2018/3/30 0030 下午 4:32
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ShiroUser addMerchantManager(MerchantManage merchantManage) {
        Merchant merchant = merchantMapper.queryMerchantForManager(merchantManage.getMerchantId());
        if (null == merchant || "".equals(merchant)) {
            throw new RuntimeException("没有当前管理员用户，请于管理员联系！");
        }
        ShiroUser shiroUser = new ShiroUser();
        shiroUser.setType(merchant.getMerchantType());
        shiroUser.setTypeIndustry(merchant.getMerchantIndustry());
        shiroUser.setEmail(merchantManage.getAccountEmail());
        shiroUser.setIsAdmin(1);
        shiroUser.setName(merchantManage.getAccountName());
        shiroUser.setPhone(merchantManage.getAccountTel());
        shiroUser.setShowImge(merchantManage.getAccountTopMapurl());
        shiroUser.setStatus(1);
        shiroUser.setTypeId(merchantManage.getMerchantId());
        shiroUser.setUsername(merchantManage.getAccountUser());
        return shiroUser;
    }

    /**
     * @param merchantManage
     * @ClassName: MerchantManageServiceImpl
     * @Return: int
     * @Decription: 修改管理员的基本信息
     * @CreateDate: Created in 2018/1/18 0018 下午 6:47
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public int updateMerchantManage(MerchantManage merchantManage) {
        //修改管理员的基本信息
        ShiroUser shiroUser = addMerchantManager(merchantManage);
        //查询得到需要修改的管理员信息
        MerchantManageVo merchantManageVo = merchantManageMapper.queryMerchantManage(merchantManage.getMerchantId());
        //设定修改主键
        shiroUser.setId(merchantManage.getUserId());
        log.info("shiroUser的用户为" + JSONObject.toJSONString(shiroUser));
        merchantManage.setUserId(merchantManageVo.getUserId());
        merchantManage.setCreatedBy(merchantManageVo.getCreatedBy());
        merchantManage.setCreatedDate(merchantManageVo.getCreatedDate());
        int flag = merchantManageMapper.updateByPrimaryKey(merchantManage);
        ResultMessage<Integer> resultMessage = iLoginFeign.editUser(shiroUser);
        if (resultMessage.getCode() != DEALER_FEGIN_SERVICE_SUCCESS_CODE) {
            throw new RuntimeException(resultMessage.getMessage().toString());
        }
        return flag;
    }


    /**
     * @param id
     * @ClassName: MerchantManageServiceImpl
     * @Return: int
     * @Decription: 重置管理员密码
     * @CreateDate: Created in 2018/1/19 0019 上午 11:19
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage resetManagePassword(Long id) {
        //int flag = merchantManageMapper.updatePassword(id);
        MerchantManage merchantManage = (MerchantManage) selectByPrimaryKey(id);
        ResultMessage resultMessage = iLoginFeign.resetPassword(merchantManage.getUserId());
        if (resultMessage.getCode() != DEALER_FEGIN_SERVICE_SUCCESS_CODE) {
            throw new RuntimeException(resultMessage.getMessage().toString());
        }
        return resultMessage;
    }

    /**
     * @param id
     * @ClassName: MerchantManageServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage<com.cloud.drore.eboss.operation.entity.MerchantManage>
     * @Decription:
     * @CreateDate: Created in 2018/1/26 0026 下午 5:59
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<MerchantManage> findMerchant(Long id) {
        return new ResultMessage(merchantManageMapper.selectByPrimaryKey(id));
    }
}
